package com.ruoyi.hatha.service.impl;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.TextStyle;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hatha.mapper.PKcbMapper;
import com.ruoyi.hatha.domain.PKcb;
import com.ruoyi.hatha.service.IPKcbService;

/**
 * 课程表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class PKcbServiceImpl implements IPKcbService 
{
    @Autowired
    private PKcbMapper pKcbMapper;

    /**
     * 查询课程表
     * 
     * @param id 课程表主键
     * @return 课程表
     */
    @Override
    public PKcb selectPKcbById(Long id)
    {
        return pKcbMapper.selectPKcbById(id);
    }

    /**
     * 查询课程表列表
     * 
     * @param pKcb 课程表
     * @return 课程表
     */
    @Override
    public List<PKcb> selectPKcbList(PKcb pKcb)
    {

        LocalDate today = LocalDate.now(); // 获取今天的日期
        LocalDate yesterday = today.minusDays(1); // 获取昨天的日期

        // 将LocalDate转换为LocalDateTime（假设时间为午夜0点）
        LocalDateTime yesterdayMidnight = yesterday.atStartOfDay();

        // 将LocalDateTime转换为Instant
        Instant instant = yesterdayMidnight.atZone(ZoneId.systemDefault()).toInstant();

        // 将Instant转换为Date
        Date date = Date.from(instant);

        pKcb.setDateStr(date);

        List<PKcb> list = pKcbMapper.selectPKcbList(pKcb);

        if(list.size()==0){
            getDateInfo(null,list.size());
            list = pKcbMapper.selectPKcbList(pKcb);
        }else if(list.size() < 10){
            getDateInfo(list.get(list.size()-1).getDateStr(),list.size());
            list = pKcbMapper.selectPKcbList(pKcb);
        }

        return list;
    }

    private void getDateInfo(Date dateStr, int size) {
        if(dateStr==null&&size==0){
            LocalDate today = LocalDate.now(); // 获取今天的日期
            for (int i = 0; i < (10 - size); i++) { // 包括今天在内的未来10天
                LocalDate futureDate = today.plusDays(i); // 获取未来某一天的日期

                LocalDateTime futureDateMidnight = futureDate.atStartOfDay();
                // 将LocalDateTime转换为Instant
                Instant instant = futureDateMidnight.atZone(ZoneId.of("Asia/Shanghai")).toInstant();
                // 将Instant转换为Date
                Date date = Date.from(instant);
                DayOfWeek weekday = futureDate.getDayOfWeek(); // 获取星期几

                String weekdayName = weekday.getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);

                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
                String formattedDate = sdf.format(date);


                PKcb kcb = new PKcb();
                kcb.setDateStr(date);
                kcb.setJian(formattedDate);
                kcb.setZhou(weekdayName.replace("星期", "周"));
                kcb.setCreateTime(new Date());

                pKcbMapper.insertPKcb(kcb);

            }
        }else{
            Integer num = size;
            Instant instant1 = dateStr.toInstant();
            ZonedDateTime zonedDateTime = instant1.atZone(ZoneId.of("Asia/Shanghai"));

            LocalDate localDate = zonedDateTime.toLocalDate();

            for (int i = 1; i <= (10 - num); i++) { // 包括今天在内的未来10天
                LocalDate futureDate = localDate.plusDays(i); // 获取未来某一天的日期

                LocalDateTime futureDateMidnight = futureDate.atStartOfDay();
                // 将LocalDateTime转换为Instant
                Instant instant = futureDateMidnight.atZone(ZoneId.of("Asia/Shanghai")).toInstant();
                // 将Instant转换为Date
                Date date = Date.from(instant);
                DayOfWeek weekday = futureDate.getDayOfWeek(); // 获取星期几

                String weekdayName = weekday.getDisplayName(TextStyle.SHORT, Locale.SIMPLIFIED_CHINESE);

                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
                String formattedDate = sdf.format(date);

                PKcb kcb = new PKcb();
                kcb.setDateStr(date);
                kcb.setJian(formattedDate);
                kcb.setZhou(weekdayName);
                kcb.setCreateTime(new Date());

                pKcbMapper.insertPKcb(kcb);
            }
        }
    }

    /**
     * 新增课程表
     * 
     * @param pKcb 课程表
     * @return 结果
     */
    @Override
    public int insertPKcb(PKcb pKcb)
    {
        return pKcbMapper.insertPKcb(pKcb);
    }

    /**
     * 修改课程表
     * 
     * @param pKcb 课程表
     * @return 结果
     */
    @Override
    public int updatePKcb(PKcb pKcb)
    {
        return pKcbMapper.updatePKcb(pKcb);
    }

    /**
     * 批量删除课程表
     * 
     * @param ids 需要删除的课程表主键
     * @return 结果
     */
    @Override
    public int deletePKcbByIds(Long[] ids)
    {
        return pKcbMapper.deletePKcbByIds(ids);
    }

    /**
     * 删除课程表信息
     * 
     * @param id 课程表主键
     * @return 结果
     */
    @Override
    public int deletePKcbById(Long id)
    {
        return pKcbMapper.deletePKcbById(id);
    }
}
